package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.Student;
import com.example.demo.entity.Teacher;
import com.example.demo.entity.User;
import com.example.demo.service.ITeacherService;
import com.example.demo.service.IUserService;
import com.example.demo.utils.AjaxJson;
import com.example.demo.utils.UserContext;
import com.example.demo.vo.teacherAddVo;
import com.example.demo.vo.teacherSelectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 教师信息表 前端控制器
 * </p>
 *
 * @author
 * @since 2025-11-10
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    ITeacherService teacherService;

    @Autowired
    IUserService userService;

    @GetMapping("/get/my")
    public AjaxJson getMy() {
        User user = UserContext.getCurrentUser();
        Teacher teacher = teacherService.getOne(new QueryWrapper<Teacher>().eq("user_id", user.getUserId()));

        if (teacher == null) {
            return AjaxJson.getError("用户不存在");
        }
        return AjaxJson.getSuccessData(teacher.getTeacherId());
    }

    /**
     * 管理员对所有教师的增删改查
     */
    @RequestMapping("/add")
    public AjaxJson add(@RequestBody teacherAddVo teacher) {
        AjaxJson ajaxJson = userService.addRegisterByTeacher(teacher);
        if (ajaxJson.getCode() != 200) {
            return ajaxJson;
        } else {
            User data = ajaxJson.getData(User.class);
            Teacher teacher1 = new Teacher();
            teacher1.setUserId(data.getUserId());
            teacher1.setCreateTime(LocalDateTime.now());
            teacher1.setUpdateTime(LocalDateTime.now());
            teacher1.setTitle(teacher.getTitle());
            teacher1.setDepartment(teacher.getDepartment());
            teacher1.setTeacherNo(teacher.getTeacherNo());
            boolean save = this.teacherService.update(teacher1, new QueryWrapper<Teacher>().eq("user_id", data.getUserId()));
            if(save){
                return AjaxJson.getSuccess("添加成功");
            }else {
                return AjaxJson.getError("添加失败");
            }
        }
    }

    @RequestMapping("/delete")
    public AjaxJson delete(@RequestParam Integer id) {
        Teacher teacher = teacherService.getById(id);
        if (teacher == null) {
            return AjaxJson.getError("用户不存在");
        }
        return teacherService.removeById(id) ? AjaxJson.getSuccess("删除成功") : AjaxJson.getError("删除失败");
    }

    @RequestMapping("/update")
    public AjaxJson update(@RequestBody Teacher teacher) {
        return teacherService.updateById(teacher) ? AjaxJson.getSuccess("修改成功") : AjaxJson.getError("修改失败");
    }

    @RequestMapping("/query")
    public AjaxJson query() {
        List<teacherSelectVo> list = teacherService.list().stream().map(teacher -> {
            teacherSelectVo teacherSelectVo = new teacherSelectVo();
            teacherSelectVo.setTeacherId(teacher.getTeacherId());
            teacherSelectVo.setUserId(teacher.getUserId());
            teacherSelectVo.setTeacherNo(teacher.getTeacherNo());
            teacherSelectVo.setTitle(teacher.getTitle());
            teacherSelectVo.setDepartment(teacher.getDepartment());
            teacherSelectVo.setCreateTime(teacher.getCreateTime());
            User byId = userService.getById(teacher.getUserId());
            teacherSelectVo.setUsername(byId.getUsername());
            teacherSelectVo.setRealName(byId.getRealName());
            teacherSelectVo.setGender(byId.getGender());
            teacherSelectVo.setPhone(byId.getPhone());
            teacherSelectVo.setEmail(byId.getEmail());
            return teacherSelectVo;
        }).toList();
        return AjaxJson.getPageData((long) list.size(), list);
    }

    @RequestMapping("/query/id")
    public AjaxJson query(@RequestParam Integer id) {
        return AjaxJson.getSuccessData(teacherService.getById(id));
    }


    @RequestMapping("/delete/teacherNo")
    public AjaxJson deleteByTeacherNo(@RequestParam String teacherNo) {
        return teacherService.deleteByTeacherNo(teacherNo);
    }

    @RequestMapping("/query/TeacherNo")
    public AjaxJson queryByTeacherNo(@RequestParam String teacherNo) {
        return AjaxJson.getSuccessData(teacherService.listByTeacherNo(teacherNo));
    }
}
